﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Tools;
using JBeamLibrary.VehicleParts.Interfaces;
using Newtonsoft.Json.Linq;

namespace JBeamLibrary.VehicleParts.Elements
{
    public class Node : VehicleElement, INumeric
    {
        private string _id;

        #region Fields

        #endregion

        #region Properties

        /// <summary>Gets or sets the node ID</summary>
        public string Id
        {
            get { return _id; }
            set
            {
                if (value == _id) return;

                var oldId = _id;
                var oldNumId = NumericId;
                _id = value;

                OnPropertyChanged(oldId, value);
                OnPropertyChanged(oldNumId, NumericId, "NumericId");
            }
        }

        /// <summary>Gets or sets the node X position</summary>
        public float PosX
        {
            get;
            set;
        }

        /// <summary>Gets or sets the node Y position</summary>
        public float PosY
        {
            get;
            set;
        }

        /// <summary>Gets or sets the node Z position</summary>
        public float PosZ
        {
            get;
            set;
        }

        /// <summary>Gets the numeric representation of this node (if applies)</summary>
        public int NumericId
        {
            get
            {
                if (string.IsNullOrEmpty(Id)) return 0;
                var numStr = string.Empty;
                foreach (var c in Id)
                {
                    if (!char.IsDigit(c))
                    {
                        numStr = string.Empty;
                        continue;
                    }
                    numStr += c;
                }
                return string.IsNullOrEmpty(numStr) ? 0 : numStr.To<int>();
            }
        }

        #endregion

        #region CTORs

        public Node()
        {
            PosX = float.MinValue;
            PosY = float.MinValue;
            PosZ = float.MinValue;
        }

        public Node(string id, float x, float y, float z)
        {
            Id = id;
            PosX = x;
            PosY = y;
            PosZ = z;
        }

        #endregion

        #region Overrides

        public override IVehicleElement FromJBeam(JToken token)
        {
            var arr = token as JArray;
            if (arr == null) return this;

            Id = token[0].FromJBeam<string>();
            PosX = token[1].FromJBeam<float>();
            PosY = token[2].FromJBeam<float>();
            PosZ = token[3].FromJBeam<float>();

            return this;
        }

        public override JToken ToJBeam()
        {
            return new JArray { Id, PosX, PosY, PosZ };
        }

        public override string ToString()
        {
            return string.Format("{0} {{{1},{2},{3}}}", Id, PosX, PosY, PosZ);
        }

        #endregion
    }
}
